/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.management;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.ImageUtils;

import java.util.List;

public class TeamMemberListAdapterHolder extends RecyclerView.Adapter {

    private List<PersonUserBean> memberList;
    private OnItemClickListener mItemClickListener;
    private Context mContext;

    public TeamMemberListAdapterHolder(Context context, List<PersonUserBean> memberList) {
        this.mContext = context;
        this.memberList = memberList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_structure_oddpeople_item, parent, false);
        return new PeopleViewHolder(sView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PeopleViewHolder) {
            PeopleViewHolder contentHolder = (PeopleViewHolder) holder;
            PersonUserBean user = memberList.get(position);
            contentHolder.tvPersonName.setText(user.getShowName());
            ImageUtils.glideThumbViewByUrl(mContext, contentHolder.mvPersonLogo, user.getUserLogoUrl());
            if (!TextUtils.isEmpty(user.getDepartmentPowerCode()) && !user.getDepartmentPowerCode().equals(ConstantsState.DEPARTMENT_NORMAL_POWER)) {
                contentHolder.powerTxt.setVisibility(View.VISIBLE);
                contentHolder.powerTxt.setText(user.getDepartmentPowerMsg());
            }
        }
    }

    @Override
    public int getItemCount() {
        return memberList.size();
    }


    public class PeopleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView mvPersonLogo;
        TextView tvPersonName, powerTxt;

        public PeopleViewHolder(View view) {
            super(view);
            mvPersonLogo = (ImageView) view.findViewById(R.id.struc_user_head_logo);
            tvPersonName = (TextView) view.findViewById(R.id.struc_user_name);
            powerTxt = (TextView) view.findViewById(R.id.user_power);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }


    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
